package com.ljf.tijian.service;

import ch.qos.logback.core.pattern.FormatInfo;
import com.ljf.tijian.dto.CalendarRequestDto;
import com.ljf.tijian.dto.CalendarResponseDto;
import com.ljf.tijian.dto.OrdersMapperDto;
import com.ljf.tijian.mapper.HospitalMapper;
import com.ljf.tijian.mapper.OrdersMapper;
import com.ljf.tijian.service.Impl.CalerdarServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Service
public class CalendarService implements CalerdarServiceImpl {

    @Autowired
    private HospitalMapper hospitalMapper;
    @Autowired
    private OrdersMapper ordersMapper;


    @Override
    public List<CalendarResponseDto> getCurrentCalendarList(Integer year, Integer month) {
        List<CalendarResponseDto> currentCalendar = new ArrayList<>();

        //制作万年历
        boolean isRun = false;
        if((year%4==0&&year%100!=0) || (year%400==0)){
            isRun = true;
        }

        //计算从1900-01-01到现在的天数
        int totalDays = 0;
        for (int i = 1900; i < year; i++) {
            if((i%4==0&&i%100!=0) || (i%400==0)){
                totalDays += 366;
            }else{
                totalDays += 365;
            }
        }
        int beforeDays = 0;
        int currentDays = 0;
        for(int j = 1; j <= month; j++){
            switch (j){
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    currentDays = 31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    currentDays = 30;
                    break;
                case 2:
                    if(isRun){
                        currentDays = 29;
                    }else {
                        currentDays = 28;
                    }
                    break;
            }
            if(j<month){
                beforeDays += currentDays;
            }
        }
        totalDays += beforeDays;
        int firstDayOfMonth = totalDays%7 + 1;
        if(firstDayOfMonth == 7){
            firstDayOfMonth = 0;
        }
        for(int k = 0; k < firstDayOfMonth; k++){
            currentCalendar.add(new CalendarResponseDto());
        }
        for (int i = 1; i <= currentDays; i++) {
            String m = month<10?"0"+month:month+"";
            String d = i<10?"0"+i:i+"";
            currentCalendar.add(new CalendarResponseDto(year+"-"+m+"-"+d));
        }
        return  currentCalendar;
    }

    @Override
    public List<CalendarResponseDto> getCalendarList30(Integer hpId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();

        //获取一个临时集合,作为Orders表中查寻的参数
        List<OrdersMapperDto> paramentlist = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            OrdersMapperDto dto = new OrdersMapperDto();
            calendar.add(Calendar.DATE,1);
            dto.setDate(sdf.format(calendar.getTime()));
            dto.setHpId(hpId);
            paramentlist.add(dto);
        }

        //根据paramList参数,查询30天渔业的日期中,每一天已经预约的人数
        //查询结果CalendarResponseDto中的属性ymd(日期),existing(已预约的票数) 额外属性:total(总票)remainder(余票)
        List<CalendarResponseDto> calendarList30 = ordersMapper.listOrdersAppointmentNumber(paramentlist);

        //根据医院编号,获取预约规则,获取每天最多的预约人数
        String[] strArr = hospitalMapper.selectByPrimaryKey(hpId).getRule().split(",");

        //继续迭代calendarList30中的total和calendar属性
        for(CalendarResponseDto cd : calendarList30){
            //将预约日期转换为Calendar对象
            try {
                calendar.setTime(sdf.parse(cd.getYmd()));
            }catch (ParseException e){
                e.printStackTrace();
            }
            //通过Calendar对象的get方法获取某天为星期几,就可以通过预约规则获取当天的预约人数
            int total = Integer.parseInt(strArr[calendar.get(Calendar.DAY_OF_WEEK)-1]);
            cd.setTotal(total);
            cd.setRemainder(total-cd.getExisting());
        }
        return calendarList30;
    }

    @Override
    public List<CalendarResponseDto> listAppointmentCalendar(CalendarRequestDto calendarRequestDto) {
        //获取预期的日期列表
        List<CalendarResponseDto> calendarList30 = getCalendarList30(calendarRequestDto.getHpId());
        //获取当前日历(当前日历的其他属性需从calendarList30中获取)
        List<CalendarResponseDto> currentCalendarList = getCurrentCalendarList(calendarRequestDto.getYear(),calendarRequestDto.getMonth());

        //给当前日历填充total,existing,remainder属性
        for(CalendarResponseDto current: currentCalendarList){
            for(CalendarResponseDto calender : calendarList30){
                if(current.getYmd()!=null){
                    if(current.getYmd().equals(calender.getYmd())){
                        current.setTotal(calender.getTotal());
                        current.setExisting(calender.getExisting());
                        current.setRemainder(calender.getRemainder());
                    }
                }
            }
        }

        return currentCalendarList;
    }
}
